package pl.smath.view.client;

import gwt.g2d.client.graphics.Surface;
import pl.smath.expression.model.Term;
import pl.smath.expression.parser.ExpressionParser;
import pl.smath.expression.parser.ExpressionParserException;
import pl.smath.view.renderer.TermRenderer;
import pl.smath.view.renderer.TermRendererException;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;

public class VariableViewSite {
	private final FlexTable termTable = new FlexTable();
	private final VerticalPanel mainPanel = new VerticalPanel();
	  
	private final String[] variableExpressions = {"x", "y", "a", "b", "w", "z", "c"};
	private final ExpressionParser parser = new ExpressionParser();
	
	//string value = com.google.gwt.user.client.Window.Location.getParameter("orderId")
	
	/**
	 * This is the entry point method.
	 */
	public VerticalPanel getPanel() {
		
	    // Assemble Main panel.
	    mainPanel.add(termTable);
	    
		termTable.setText(0, 0, "Expression");
		termTable.setText(0, 1, "Term.toString()");
		termTable.setText(0, 2, "Rendered canvas");
		
		String result = null;
		for (String expression : variableExpressions) {
			Surface surface = null;
			try {
				Term term = parser.parseText(expression);
				result = term.toString();
				surface = TermRenderer.renderCanvas(term);
			} catch (TermRendererException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExpressionParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Integer rows = termTable.getRowCount();
			Integer row = rows+1;
			termTable.setText(row, 0, expression);
			termTable.setText(row, 1, result);
			termTable.setWidget(row, 2, surface);
		}
	    
	    return mainPanel;
	}
}
